var names=[];
var roomIndex=1;//房间号
var list={};
var gameData={};
function router(app) {
	//跨域设置
	app.all('*', function(req, res, next) {
	    res.header("Access-Control-Allow-Origin", "*");
	    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
	    res.header("Access-Control-Allow-Headers", "X-Requested-With");
	    res.header('Access-Control-Allow-Headers', 'Content-Type');
	    next();
	});
	app.post('/addUser', function(req, res) {
		var name=req.body.name;
		var ip=req.ip;
		if(!name){
			res.end('error name');
		}else{
			if(names.indexOf(name)>-1){
				res.json({
					flag:false,
					msg:'姓名已存在'
				});
			}else{
				names.push(name);
				res.json({
					flag:true,
					msg:'添加成功'
				});
			}
			
		}
	})
	app.get('/saveroom',function(req,res){
		roomIndex++;
		res.send('ok');
	})
	app.get('/getroom',function(req,res){
		res.json({
			flag:true,
			data:roomIndex
		})
	})
	app.post('/saveGameData',function(req,res){
		var room=req.body.room,
			start=req.body.start,
			data=req.body.data;
		var th_room=gameData['room'+room]={};
		th_room.start=start;
		th_room.data=data;
		res.send('ok');
	})
	app.post('/getGameData',function(req,res){
		var room=req.body.room;
		var _json=gameData['room'+room] || '';
		res.json({
			flag:true,
			data:_json
		})
	})
}
module.exports.router=router;